﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using CTP.Command;
using CTP.Entitys;
using CTP.Utilty;
using CTP.ComWrap;
using CTP.Session;

namespace CTP.ComWrap
{
    /// <summary>
    /// 功能：实现COM事件处理
    /// </summary>
    [ComSourceInterfaces(typeof(IComEvent))]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    [Guid("7cbd8ec1-4038-4283-b69a-d83eb31293b6")]
    public class ComEvent : ICTPCallbackHandle,IDisposable
    {
        #region 事件定义
        public event OnErrorHandle OnErrorEvent;
        public event OnLoginHandle OnLoginEvent;
        public event OnLogoutHandle OnLogoutEvent;
        public event OnLoginQouteHandle OnLoginQouteEvent;
        public event OnLogoutQouteHandle OnLogoutQouteEvent;
        public event OnAccountSummaryHandle OnAccountSummaryEvent;
        public event OnQuerySettlementInfoHandle OnQuerySettlementInfoEvent;
        public event OnConfirmSettlementInfoHandle OnConfirmSettlementInfoEvent;
        public event OnOrderStatusHandle OnOrderStatusEvent;
        public event OnOrderFilledHandle OnOrderFilledEvent;
        public event OnQueryPositionHandle OnQueryPositionEvent;
        public event OnQueryOrderHandle OnQueryOrderEvent;
        public event OnQueryFilledHandle OnQueryFilledEvent;
        public event OnHistoricalOrderHandle OnHistoricalOrderEvent;
        public event OnHistoricalFilledHandle OnHistoricalFilledEvent;
        public event OnQouteHandle OnQouteEvent;
        #endregion

        #region 构造与析构
        public ComEvent()
        {
            ResponseManager.Instance.RegisterCallback(this);
        }
        public void Dispose()
        {
            try
            {
                Logger.Instance.WriteInfo("COM客户端正在析放");
                ResponseManager.Instance.UnRegisterCallback(this);
            }
            catch
            {
            }
        }
        #endregion

        #region 回调接口

        void ICTPCallbackHandle.OnError(ReturnInfo result, int requestID)
        {
            if (OnErrorEvent != null)
            {
                OnErrorEvent(
                    new ErrorEventArgs()
                    {
                        ReturnInfo = result,
                        RequestID = requestID
                    });
            }
        }

        void ICTPCallbackHandle.OnLogin(ReturnInfo result, LoginInfo loginInfo, int requestId)
        {
            if (OnLoginEvent != null)
            {
                OnLoginEvent(new LoginEventArgs()
                    {
                        ReturnInfo = result,
                        LoginInfo = loginInfo,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnLogout(ReturnInfo result, string tradingAccount, int requestId)
        {
            if (OnLogoutEvent != null)
            {
                OnLogoutEvent(new LogoutEventArgs()
                    {
                        ReturnInfo = result,
                        TradingAccount = tradingAccount,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnAccountSummary(ReturnInfo result, AccountInformation summary, int requestId)
        {
            if (OnAccountSummaryEvent != null)
            {
                OnAccountSummaryEvent(new AccountSummaryEventArgs()
                    {
                        ReturnInfo = result,
                        AccountInformation = summary,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnOrderStatus(ReturnInfo result, OrderStatus ord, int requestId)
        {
            if (OnOrderStatusEvent != null)
            {
                OnOrderStatusEvent(new OrderStatusEventArgs()
                    {
                        ReturnInfo = result,
                        OrderStatus = ord,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnOrderFilled(ReturnInfo result, OrderReport ord)
        {
            if (OnOrderFilledEvent != null)
            {
                OnOrderFilledEvent(new OrderFilledEventArgs()
                    {
                        ReturnInfo = result,
                        Report = ord
                    });
            }
        }

        void ICTPCallbackHandle.OnQuerySettlementInfo(ReturnInfo result, SettlementInfoConfirm confirmInfo, int requestId)
        {
            if (OnQuerySettlementInfoEvent != null)
            {
                OnQuerySettlementInfoEvent(new QuerySettlementInfoEventArgs()
                    {
                        ReturnInfo = result,
                        ConfirmInfo = confirmInfo,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnConfirmSettlementInfo(ReturnInfo result, int requestId)
        {
            if (OnConfirmSettlementInfoEvent != null)
            {
                OnConfirmSettlementInfoEvent(new ConfirmSettlementInfoEventArgs()
                    {
                        ReturnInfo = result,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnPosition(ReturnInfo result, PositionInformation[] positions, int requestId)
        {
            if (OnQueryPositionEvent != null)
            {
                OnQueryPositionEvent(new QueryPositionEventArgs()
                    {
                        ReturnInfo = result,
                        Positions = positions,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnQueryOrder(ReturnInfo result, Order[] orders, int requestId)
        {
            if (OnQueryOrderEvent != null)
            {
                OnQueryOrderEvent(new QueryOrderEventArgs()
                    {
                        ReturnInfo = result,
                        Orders = orders,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnHistoricalOrder(ReturnInfo result, Order[] orders, int requestId)
        {
            if (OnHistoricalOrderEvent != null)
            {
                OnHistoricalOrderEvent(new HistoricalOrderEventArgs()
                    {
                        ReturnInfo = result,
                        Orders = orders,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnQueryFilled(ReturnInfo result, OrderReport[] orders, int requestId)
        {
            if (OnQueryFilledEvent != null)
            {
                OnQueryFilledEvent(new QueryFilledEventArgs()
                    {
                        ReturnInfo = result,
                        Reports = orders,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnHistoricalFilled(ReturnInfo result, OrderReport[] orders, int requestId)
        {
            if (OnHistoricalFilledEvent != null)
            {
                OnHistoricalFilledEvent(new HistoricalFilledEventArgs()
                    {
                        ReturnInfo = result,
                        Reports = orders,
                        RequestID = requestId
                    });
            }
        }

        void ICTPCallbackHandle.OnLoginQoute(ReturnInfo result, LoginInfo loginInfo, int requestId)
        {
            if (OnLoginQouteEvent != null)
            {
                OnLoginQouteEvent(new LoginEventArgs()
                {
                    ReturnInfo = result,
                    LoginInfo = loginInfo,
                    RequestID = requestId
                });
            }
        }

        void ICTPCallbackHandle.OnLogoutQoute(ReturnInfo result, string tradingAccount, int requestId)
        {
            if (OnLogoutQouteEvent != null)
            {
                OnLogoutQouteEvent(new LogoutEventArgs()
                {
                    ReturnInfo = result,
                    TradingAccount = tradingAccount,
                    RequestID = requestId
                });
            }
        }

        void ICTPCallbackHandle.OnQoute(QouteInfo qouteInfo)
        {
            if (OnQouteEvent != null)
            {
                OnQouteEvent(new QouteEventArgs()
                {                    
                   QouteInfo=qouteInfo
                });
            }
        }

        #endregion
    }

    #region 对COM接口

    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    [Guid("3063ac36-8d65-4f43-9c23-c776431ca732")]
    public interface IComEvent
    {
        [DispId(1)]
        void OnErrorEvent(ErrorEventArgs e);

        [DispId(2)]
        void OnLoginEvent(LoginEventArgs e);

        [DispId(3)]
        void OnLogoutEvent(LogoutEventArgs e);

        [DispId(4)]
        void OnAccountSummaryEvent(AccountSummaryEventArgs e);

        [DispId(5)]
        void OnQuerySettlementInfoEvent(QuerySettlementInfoEventArgs e);

        [DispId(6)]
        void OnConfirmSettlementInfoEvent(ConfirmSettlementInfoEventArgs e);

        [DispId(7)]
        void OnOrderStatusEvent(OrderStatusEventArgs e);

        [DispId(8)]
        void OnOrderFilledEvent(OrderFilledEventArgs e);

        [DispId(9)]
        void OnQueryPositionEvent(QueryPositionEventArgs e);

        [DispId(10)]
        void OnQueryOrderEvent(QueryOrderEventArgs e);

        [DispId(11)]
        void OnQueryFilledEvent(QueryFilledEventArgs e);

        [DispId(12)]
        void OnHistoricalOrderEvent(HistoricalOrderEventArgs e);

        [DispId(13)]
        void OnHistoricalFilledEvent(HistoricalFilledEventArgs e);

        [DispId(14)]
        void OnQouteEvent(QouteEventArgs e);
       
        [DispId(15)]
        void OnLoginQouteEvent(LoginEventArgs e);

        [DispId(16)]
        void OnLogoutQouteEvent(LogoutEventArgs e);
    }

    #endregion
}
